var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');

var xcxcode = ""
var Cinemapojo = '';
var plandate = '';
var danprice = '';
var showdate = "";
var EventId = "";
var flag = "";
var hasSend = false;
var hasReg = false;
//获取应用实例
const app = getApp();
var clickpoint = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tel: '',
    code: '',
    btntxt: '获取验证码',
    sendflag: false,
    second: 60,
    isStop:false
  },

  /**
   * 生命周期函数--监听页 面加载
   */
  onLoad: function (options) {

    console.log(options)

    if (options["frombuy"]) {
      //TODO在锁座时跳转
      flag = "frombuy"
      Cinemapojo = JSON.parse(options.Cinemapojo);
      plandate = JSON.parse(options.plandate);
      showdate = options.showdate;
    }
    else if (options["frommy"]) {
      //TODO 从个人跳转过来 
      console.log("frommy")
      flag = "frommy"
      //DO NOTHING
    } else if (options["fromhuodongfood"]) {
      console.log("fromhuodongfood")
      flag = "fromhuodongfood";
      EventId = options.EventId;
    } else if (options["fromhuodongmovie"]) {
      flag = "fromhuodongmovie";
      EventId = options.EventId;
    }

    this.setData({ hasReg: false })
    var that = this;
    wx.login({
      success: function (res) {
        //发起网络请求  
        xcxcode = res.code
      }
    });
  },
  getCode: function () {
    var that = this;
    if (this.validatemobile()) {
      // 设置为已经发送
      // 开始计时
      // 发起请求/获取验证码
      that.setData({
            sendflag: true,
            second:60,
            isStop:false
          });
      that.countdown();
      return req.SendSms(this.data.tel).then(res => {
        console.log(res)
        if(res.data.success){
        }else{
          that.setData({
            sendflag: false,
            isStop:true
          });
           wx.showModal({
                    title: '发送失败',
                    content: res.data.data,
                    showCancel: false,
                    success: function (res) {
                    }
                })
        }
      })
    } else {
      return;
    }
  },
  // 保存手机号
  telsuccess: function () {
    console.log("点击");
    var that = this;
    var code_temp = that.data.code;
    if (this.validatemobile() && code_temp.length != 0) {
    }
  },
  // 验证手机号格式
  validatemobile: function () {
    var mobile = this.data.tel;
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        image: '/static/images/cuo.png',
        duration: 1500
      })
      return false;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        image: '/static/images/cuo.png',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号格式错误！',
        image: '/static/images/cuo.png',
        duration: 1500
      })
      return false;
    }
    return true;
  },
  // 计时
  countdown: function () {
    var that = this;
       console.log(this.data. second);
    var second =this.data. second;
    if (second == 0||this.data.isStop) {
      // console.log("Time Out...");
      that.setData({
        sendflag: false,
        second: 60,
      });
      return;
    }
    var time = setTimeout(function () {
     
      that.setData({
        second: second - 1
      });
      that.countdown();
    }, 1000)
  },
  // 获取输入的手机号
  TelInput: function (e) {
    this.setData({
      tel: e.detail.value
    })
  },
  // 获取输入的验证码
  CodeInput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  //TODO确认绑定
  tobind: function () {
    if (this.validatemobile() && this.data.code !== '' && xcxcode !== "") {
      // TODO校验验证码
      wx.showLoading({
        title: '请稍候', mask: true
      })
      return req.MiniAppRegister(xcxcode, this.data.tel, this.data.code).then(res => {
        console.log(res)
        wx.hideLoading()
        if (res.data.success && flag == "frombuy") {
          this.setData({ hasReg: true })
          app.globalData.token = res.data.data;
          wx.redirectTo({
            url: '../yingting/index?plandate=' + JSON.stringify(plandate) + '&&showdate=' + showdate + '&&Cinemapojo=' + JSON.stringify(Cinemapojo) + '&&frombuy=true'
          })
        } else if (res.data.success && flag == "frommy") {
          this.setData({ hasReg: true })
          app.globalData.token = res.data.data;

          wx.reLaunch({
            url: '../member/index',
          })
        } else if (res.data.success && flag == "fromhuodongfood") {
          wx.redirectTo({
            url: '../member/activityDetail?EventId=' + EventId + ''
          })
        } else if (res.data.success && flag == "fromhuodongmovie") {
          wx.redirectTo({
            url: '../huodong/huodongmovie?detail=' + EventId + ''
          })
        }
        if (!res.data.success) {
          wx.showModal({
            // title: res.data.data,
            content: res.data.data,
            showCancel: false,
            success: function (res) {
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '验证码不正确',
        image: '/static/images/cuo.png',
        duration: 1500
      })
    }

  }
})